package districting;

import java.util.Comparator;
import java.util.List;

import com.vividsolutions.jts.geom.Point;

public class PollingBoothDistanceComparatorC implements
      Comparator<PollingBooth> {

   private Point point;

   private List<Point> points;

   public PollingBoothDistanceComparatorC(List<Point> points, Point point) {
      this.points = points;
      this.point = point;
   }

   @Override
   public int compare(PollingBooth pb0, PollingBooth pb1) {
      double dist0 = 0;
      double minDist0 = Double.MAX_VALUE;
      double dist1 = 0;
      double minDist1 = Double.MAX_VALUE;
      for (Point point : points) {
         if (point == this.point) {
            dist0 = pb0.getPoint().distance(point);
            dist1 = pb1.getPoint().distance(point);
         } else {
            minDist0 = Math.min(minDist0, pb0.getPoint().distance(point));
            minDist1 = Math.min(minDist1, pb1.getPoint().distance(point));
         }
      }

      if (dist0 / minDist0 > dist1 / minDist1) {
         return 1;
      } else if (dist0 / minDist0 < dist1 / minDist1) {
         return -1;
      } else {
         return 0;
      }
   }

}
